package com.zhs.gccs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.annimon.stream.Optional;
import com.annimon.stream.Stream;
import com.gyf.immersionbar.ImmersionBar;
import com.zhs.gccs.R;
import com.zhs.gccs.activity.gccs.AdminGccsTaskActivity;
import com.zhs.gccs.activity.gccs.GccsUserTaskListActivity;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.model.SysRole;
import com.zhs.gccs.utils.SharedPrefHelper;
import com.zhs.gccs.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SplashActivity extends BaseActivity   {

    @BindView(R.id.iv_bg)
    ImageView iv_bg;
    private Handler mHandler = new Handler();
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            long lastLoginTime = SharedPrefHelper.getInstance().getLastLoginTime();
            long time = (System.currentTimeMillis() - lastLoginTime)/1000/3600;//s 60*60*24
            //后台 令牌有效期（默认72小时=4320分钟）  我们这里用1天
            if(TextUtils.isEmpty(SharedPrefHelper.getInstance().getUserAccount())
                    ||time>=24){
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
            }else {
//                List<String> roles = SharedPrefHelper.getInstance().getUserRoles();
//                if(roles==null||roles.size()==0){
//                    ToastUtils.toast("无权限操作，请联系管理员分配权限后重新登录！");
//                    Intent intent = new Intent(context, LoginActivity.class);
//                    startActivity(intent);
//                    return;
//                }
//                Optional<String> gccscjrRole = Stream.of(roles).
//                        filter(i -> SysRole.role_gccs_admin.equals(i)).findFirst();
//                if(gccscjrRole.isPresent()){
//                    AdminGccsTaskActivity.start(SplashActivity.this);
//                }else {
//                    GccsUserTaskListActivity.start(SplashActivity.this);
//                }
                /**/
//                Intent intent = new Intent(context, MainActivity.class);
//                startActivity(intent);
                DrawerMainActivity.start(SplashActivity.this);
            }
            finish();
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        ImmersionBar.with(this).transparentBar().statusBarDarkFont(false).init();
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.postDelayed(mRunnable, 1500);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mHandler.removeCallbacks(mRunnable);
    }
}
